package org.lanqiao.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.lanqiao.entity.AcActivity;
import org.lanqiao.entity.Activity;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface ActivityMapper {

    /**
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/6/12 10:09
     * @Param [id]
     * @explain       根据id查询活动
     * @return java.util.List<org.lanqiao.entity.Activity>
     */


    Activity   getActivityById(@Param("id") String id);

    /**
     * 功能描述 获取一个用户所有的活动
     *
     * @return java.util.List<org.lanqiao.entity.Activity>
     * @author 吴奇
     * @date 2019/5/21
     */
    List<AcActivity> getActivity(@Param("userEmail") String userEmail, @Param("name") String name);
    /**
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/6/11 23:27
     * @Param []
     * @explain
     * @return java.util.List<org.lanqiao.entity.Activity>
     */

    List<Activity> getAllSite();

    /**
     * @return void
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/5/27 16:42
     * @Param [activity]
     * @explain 新增活动
     */

    void addActivity(@Param("activity") Activity activity);


    @Select("select g.gift_name,g.gife_price,wg.gift_time,wg.gift_num,wg.total_money from gifts g right join wxuser_gift wg on wg.gift_id=g.gift_id where wg.wx_id=#{wx_id} and wg.activity_id=#{activityId}")
    List<Map>  getGiftsByUser(@Param("wx_id") String wxId, @Param("activityId") String activityId);

    @Select("SELECT s.`status_name`,\n" +
            "t.`type_name`,\n" +
            "e.`site_name`,\n" +
            "a.`activity_name`,a.`activity_income`," +
            "Date_FORMAT(a.`activity_starttime`,'%Y-%m-%d %H:%i:%s') as 'activity_starttime'," +
            "Date_FORMAT(a.`activity_endtime`,'%Y-%m-%d %H:%i:%s') as 'activity_endtime',a.`activity_id`,a.`msg_num`,\n" +
            "a.activity_status,u.user_name,\n" +
            "(SELECT COUNT(w.`activity_id`) FROM wxuser_activity w RIGHT JOIN  activitys c ON w.`activity_id`=c.`activity_id` WHERE c.`activity_id`=a.`activity_id`) AS 'count'\n" +
            "FROM activitys a LEFT JOIN STATUS s ON s.`status_id`=a.`activity_status`\n" +
            "\tLEFT JOIN TYPES t ON t.`type_id`=a.`activity_type` \n" +
            "\tLEFT JOIN sites e ON e.`site_id`=a.`activity_site`\n" +
            "\tLEFT JOIN users u ON u.user_id = a.user_id")
    public List<Map> getAllActivity();

    @Update("update activitys set activity_status='2' where activity_id=#{id}")
    public void updateAct(@Param("id") String id);

    @Select("SELECT s.`status_name`,\n" +
            "            t.`type_name`,\n" +
            "            e.`site_name`,\n" +
            "            a.`activity_name`,\n" +
            "            a.`activity_income`,\n" +
            "            Date_FORMAT(a.`activity_starttime`,'%Y-%m-%d %H:%i:%s') as 'activity_starttime',\n" +
            "            Date_FORMAT(a.`activity_endtime`,'%Y-%m-%d %H:%i:%s') as 'activity_endtime',\n" +
            "            a.`activity_id`,\n" +
            "            a.msg_num," +
            "            a.activity_status,\n" +
            "            u.user_name,\n" +
            "            (SELECT COUNT(w.`activity_id`)\n" +
            "                FROM wxuser_activity w\n" +
            "                RIGHT JOIN  activitys c ON w.`activity_id`=c.`activity_id`) AS 'count'\n" +
            "            FROM activitys a\n" +
            "            LEFT JOIN STATUS s ON s.`status_id`=a.`activity_status`\n" +
            "            LEFT JOIN TYPES t ON t.`type_id`=a.`activity_type`\n" +
            "            LEFT JOIN sites e ON e.`site_id`=a.`activity_site`\n" +
            "            LEFT JOIN users u ON u.user_id = a.user_id\n" +
            "            WHERE a.user_id = #{username}")
    List<Map> getByNameActivity(@Param("username") String username);

    /**
     * @Author 王宇
     * @Description //TODO 通过活动名来进行活动的模糊查询
     * @Date 2019/06/03 16:09
     * @Param [activityName]
     * @return org.lanqiao.entity.Activity
     */

    List<Activity> getActivityName(@Param("activityName") String activityName);

    /**
     * @Author 彭泳寒
     * @Description //TODO 根据活动id来进行活动时间查询
     */
    Activity getActivityTime(@Param("activityId") String activityId);

   /**
    *功能描述 修改活动
    * @author 吴奇
    * @date 2019/6/11
    * @return void
    */
    void updateAc(@Param("id") String id, @Param("name") String name, @Param("sndtime") String sndtime);

    /**
     *更具活动的id进行查询活动状态
     * @param activityId
     * @return 任春旭
     */
    Activity queryStatus(@Param("activityId") String activityId);

    void saveMoney(@Param("acid") String acid, @Param("income") double income);
}